<template>
  <div v-loading="loading">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('项目名称')" prop="dealName">
        <el-input
          v-model="queryParams.dealName"
          :placeholder="$t('项目名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('收款状态')" prop="collectionState">
        <el-select v-model="queryParams.collectionState" :placeholder="$t('收款状态')">
          <el-option v-for="item in collectionStateOptions" :key="item.dictValue" :value="item.dictValue" :label="item.dictLabel" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['deal:collection:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t("新增") }}</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['deal:collection:export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="exportHandle"
          >{{ $t("导出") }}</el-button>
        </el-col>
        <!--        <el-col :span="1.5">-->
        <!--          <el-button-->
        <!--            type="success"-->
        <!--            icon="el-icon-files"-->
        <!--            size="mini"-->
        <!--            :disabled="batchEditCollectionTimeParam.ids <= 0"-->
        <!--            @click="batchEditCollectionTimeOpen = true"-->
        <!--          >{{ $t("批量维护收款时间") }}</el-button>-->
        <!--        </el-col>-->
      </el-row>

      <el-table ref="table" v-loading="loading" tooltip-effect="darkTable" :data="dealCollectionList" row-key="id" border max-height="600" @select="tableSelectHandle" @select-all="tableSelectAllHandle">
        <!--        <el-table-column type="selection" width="55" align="center" :selectable="selectableFunc" />-->
        <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
          <template slot-scope="scope">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('项目名称')" align="center" prop="dealName" width="180" show-overflow-tooltip />
        <el-table-column :label="$t('项目经理')" align="center" prop="projectManagerName" width="150" show-overflow-tooltip />
        <el-table-column :label="$t('所属部门')" align="center" prop="deptName" width="110" show-overflow-tooltip />
        <el-table-column :label="$t('收款时间')" align="center" prop="collectionTime" :sortable="!!param" min-width="100">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.collectionTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('收款金额') + $t('金额单位')" align="center" prop="collectionMoney" :sortable="!!param" min-width="120">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.collectionMoney, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('是否开具发票')" align="center" prop="invoice" :formatter="invoiceFormat" min-width="110" />
        <el-table-column width="180" :label="$t('款项名称')" align="center" prop="collectionName" show-overflow-tooltip />
        <el-table-column width="180" :label="$t('收款条件')" align="center" prop="collectionTerms" show-overflow-tooltip />
        <el-table-column :label="$t('关联合同')" align="center" prop="joinContractName" width="180" show-overflow-tooltip />
        <el-table-column :label="$t('收款状态')" fixed="right" align="center" prop="collectionState" min-width="100">
          <template slot-scope="scope">
            <span :style="collectionStateColor(scope.row)">{{ collectionStateFormat(scope.row) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="50">
          <template slot-scope="scope">
            <el-dropdown @command="handleCommand($event, scope.row)">
              <i class="el-icon-more" />
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-hasPermi="['deal:collection:edit']" command="update">{{ $t('修改') }}</el-dropdown-item>
                <el-dropdown-item v-hasPermi="['deal:collection:remove']" command="delete">{{ $t('删除') }}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-form>

    <!-- 添加或修改项目收款对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealCollectionForm ref="dealCollectionForm" />
    </el-dialog>

    <el-dialog :title="$t('批量维护收款时间')" :visible.sync="batchEditCollectionTimeOpen" :close-on-click-modal="false" :lock-scroll="false" width="30%" append-to-body @close="getList">
      <el-form ref="form" v-loading="batchLoading" :model="batchEditCollectionTimeParam" label-width="120px" class="dataForm">
        <el-form-item :label="$t('日期')" prop="date" :rules="{ required: true, message: $t('必填信息'), trigger: 'blur' }">
          <el-date-picker
            v-model="batchEditCollectionTimeParam.date"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :placeholder="$t('日期')"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: right;">
        <!--保存，保存并启动流程，取消 三个按钮-->
        <SaveBtn has-permi="deal:collection" :cancel-func="() => { batchEditCollectionTimeOpen = false }" :function="batchEditCollectionTimeFunc" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  delDealCollection,
  batchEditCollectionTime,
  findWarningCollection
} from '@/api/deal/collection/dealCollection'
import dealCollectionForm from '@/views/deal/collection/dealCollectionForm'
import { selectContractByDealIdW } from '@/api/deal/contract/dealContract'

export default {
  components: {
    dealCollectionForm
  },
  props: {
    param: {
      type: Object,
      default: undefined,
      desc: '查询参数'
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 项目收款表格数据
      dealCollectionList: [],
      // 查询 收款时间
      collectionTimes: [],
      // 弹出层标题
      title: '项目收款',
      // 是否显示弹出层
      formOpen: false,
      // 团队成员遮罩层
      dealTeamOpen: false,
      // 日期范围
      dateRange: [],
      // 是否开具发票字典
      invoiceOptions: [],
      // 收款状态字典
      collectionStateOptions: [],
      // 查询参数
      queryParams: {
        collectionState: undefined,
        dealName: undefined,
        exportType: 'warning',
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 合同下拉
      contractOptions: [],
      // 团队成员项目ID参数
      dealTeamDealId: undefined,
      batchEditCollectionTimeParam: {
        ids: [],
        date: undefined
      },
      batchEditCollectionTimeOpen: false,
      batchLoading: false
    }
  },
  created() {
    this.getList()
    this.getDicts('invoice_yn').then(response => {
      this.invoiceOptions = response.data
    })
    this.getDicts('collection').then(response => {
      this.collectionStateOptions = response.data.filter(item => item.dictValue === 'YWT' || item.dictValue === 'HZ')
    })
    // 合同
    selectContractByDealIdW(this.$route.query.id).then(response => {
      this.contractOptions = response.data
    })
  },
  methods: {
    /** 查询项目收款列表 */
    getList() {
      this.batchEditCollectionTimeParam.ids = []
      this.loading = true
      findWarningCollection(this.queryParams).then(res => {
        this.total = res.total
        this.dealCollectionList = res.rows
        this.loading = false
      })
    },
    // 是否开具发票字典翻译
    invoiceFormat(row) {
      return this.selectDictLabel(this.invoiceOptions, row.invoice)
    },
    // 收款状态字典翻译
    collectionStateFormat(row) {
      return this.selectDictLabel(this.collectionStateOptions, row.collectionState)
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        collectionName: undefined,
        joinContract: undefined,
        collectionTime: undefined,
        collectionMoney: undefined,
        invoice: '0',
        collectionState: '0',
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.collectionTimes = []
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealCollectionForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealCollectionForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delDealCollection(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/collection/dealCollection/exportCollectionSummary', this.queryParams)
    },
    // 不同的收款类型返回不同的颜色
    collectionStateColor(item) {
      if (item.collectionState === 'YSK') {
        return { color: 'green' }
      } else if (item.collectionState === 'YWT') {
        return { color: '#FAD033' }
      } else if (item.collectionState === 'HZ') {
        return { color: 'red' }
      } else {
        return ''
      }
    },
    // 维护团队成员
    assertDealTeam(row) {
      this.dealTeamDealId = row.dealId
      this.dealTeamOpen = true
    },
    tableSelectHandle(selection) {
      this.batchEditCollectionTimeParam.ids = selection.map(ele => {
        return ele.id
      })
    },
    tableSelectAllHandle(selection) {
      this.batchEditCollectionTimeParam.ids = selection.map(ele => {
        return ele.id
      })
    },
    selectableFunc(row) {
      return row.collectionState !== 'YSK'
    },
    batchEditCollectionTimeFunc() {
      this.batchLoading = true
      batchEditCollectionTime(this.batchEditCollectionTimeParam).then(res => {
        if (res.code === 200) {
          this.msgSuccess(this.$t('保存成功'))
          this.batchLoading = false
          this.batchEditCollectionTimeOpen = false
        }
      })
    },
    handleCommand(val, row) {
      switch (val) {
        case 'update':
          this.updateHandle(row)
          break
        case 'delete':
          this.deleteHandle(row)
          break
        case 'assertTeam':
          this.assertDealTeam(row)
          break
      }
    }
  }
}
</script>

<style scoped>

</style>
